<?php
//filename : requeststu.php

require_once APPPATH.'controllers/gnc_mng'.EXT;

class REQUESTSTU extends Gnc_mng {
	protected $model_path = 'stu/m_requeststu';
	
	//dioverride agar saat add data, status dan nip_pe_request tidak ditampilkan
	protected function populate_add_content_data(&$content_data = NULL, &$external_data = NULL) {
		$mode = $external_data['add_data']['mode'];		
		
		$nav = $external_data['add_data']['nav_data'];
		
		$options = $external_data['add_data']['options'];
		
		//bagian yang dioverride
		$myquery = "select id,kategori_tugas,perihal_tugas,lokasi_tugas, dt_berangkat,  dt_kembali, nama_bertugas,keterangan from stu_t_request"; 
		// get metadata only
		if (isset($options['type']) && (strcasecmp($options['type'], 'detail')==0)) {
			$master_data = $this->model->get_single($options['keys']);
			$data = $this->detail_model->get_from_sql($myquery, true, false); //bagian yang dioverride
			$record = array();
			foreach ($data['metadata']['keys']['foreign'][$data['metadata']['pri_table']] as $fi => $fv) {
				foreach ($fv['fk_value'] as $idx => $val) {
					if (in_array($val, $master_data['metadata']['fields_list'])) {
						$record[$idx] = $master_data['data']['current'][$val];
					}
				}
			}
			$data['data']['current'] = $record;
			
		} else {
			$data = $this->model->get_from_sql($myquery, true, false);//bagian yang dioverride
			//print_r($data);
		}

		$this->populate_add_actions($actions, $_ = array('mode' => $mode, 'content_data' => compact('nav', 'data', 'options', 'actions')));		

		if (isset($options['type']) && (strcasecmp($options['type'], 'detail')==0)) {
			$mode .= '_detail';
		}		
		
		$form = array('action' => get_unit_controller($this).'/process_'.$this->add_attr[$mode]['function'], 'attributes' => array());
		
		$content_data = compact('nav', 'options', 'data', 'actions', 'form');
	}

	//dioverride agar saat add data,  nip_pe_request otomatis berisi nip user login
	protected function process_add_input(&$add_data = NULL) {		
		$data = array();	
		$options = array();
		
		$add_data['options'] = $this->common->get_options_from_uri();	
		
		foreach ($_POST as $idx => $val) {
			if (strcmp(substr($idx, 0, 1), '_')!=0) {
				$data[$idx] = $val;
			} else {
				if (strcmp(substr($idx, 0, 2), '__')!=0) {
					$options[substr($idx, 1)] = $val;
				}
			}
		}
		//print_r_pre($options);
		//print_r_pre($options);
		
		$myKeyUser=$this->session->userdata('user');
		$query= "select nip from sdm_m_pegawai where sdm_m_pegawai.key_sys_user ='".$myKeyUser."'";
		$myniparr = $this->db->query($query);
		$myniparr = $myniparr->result_array();
		$mynip=$myniparr[0];
		
		//echo $myKeyUser." fiuhhh".$mynip['nip'];
		$data['nip_pe_request'] = $mynip['nip'];
		foreach ($_FILES as $idx => $val) {
			if (!$val['error']) {
				$file_contents = file_get_contents($val['tmp_name']);
				$data[$idx] = $file_contents;
				
				if (isset($options['filename_'.$idx])) {
					$filename_field = $options['filename_'.$idx];
					$data[$filename_field] = $val['name']; 
					
				}
			}
		}
		
		foreach ($data as $idx => $val) {
			if (isset($options['null_'.$idx])) {
				$data[$idx] = null;
			}
		}
		
		//print_r_pre($data);
		//exit;
		$add_data['data'] = $data;
		//print_r_pre($add_data);
	}
}
?>
